﻿// Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
// for details on configuring this project to bundle and minify static web assets.

// Write your JavaScript code.
jQuery(document).ready(function ($) {
    layui.use('table', function () {
        var table = layui.table,
            form = layui.form;
    });

    $("body").on('click', "a.ajaxLink", function (e) {
        e.preventDefault();
        var t = $(this);
        var title = t.data("title");
        var url = t.data("href");
        var index = layer.open({
            type: 2,
            title: title,
            content: url
        });
        layer.full(index);
    });

    $("body").on('submit', "form.ajaxTable", function (e) {
        e.preventDefault();
        var form = $(this);
        var d = {};
        var t = form.serializeArray();
        $.each(t, function () {
            d[this.name] = this.value;
        });
        layui.table.reload(form.data("target"), {
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: d
        });
    });
    $("body").on('submit', "form.ajaxForm", function (e) {
        ajaxSubmit($(this), null);
        return false;
    });

    var ajaxSubmit = function (ajaxForm, conform) {

        var index1 = parent.layer.load(0, { shade: false }); //0代表加载的风格，支持0-2
        ajaxForm.ajaxSubmit({
            data: {
                conform: conform,
            },
            success: function (msg) {
                if (msg.success == true) {
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);

                    var mg = msg.errorMsg;
                    if (mg == null) {
                        mg = "保存成功";
                    }
                    parent.layer.msg(mg, { icon: 1, time: 1000 });
                    var form = parent.$('form.ajaxTable');
                    if (form.length > 0) {
                        var d = {};
                        var t = form.serializeArray();
                        $.each(t, function () {
                            d[this.name] = this.value;
                        });
                        parent.layui.table.reload(form.data("target"), {
                            page: {
                                curr: 1 //重新从第 1 页开始
                            },
                            where: d
                        });
                    } else {
                        if (index==undefined) {
                            window.location.reload();
                        } else {
                            parent.window.location.reload();
                        }
                        //parent.$('a.btn-success i.Hui-iconfont').click();
                    }
                    //parent.window.location.reload();

                } else if (msg.success == false) {
                    var mg = msg.errorMsg;
                    if (mg == null) {
                        mg = "保存失败!";
                    }
                    if (msg.conform) {
                        layer.confirm(mg, { icon: 3, title: '提示' }, function (index) {
                            layer.close(index);
                            ajaxSubmit(ajaxForm, true);
                        });
                    } else {
                        layer.msg(mg, { icon: 6, time: 3000 });

                        console.error(mg);
                    }
                } else {
                    var index = parent.layer.getFrameIndex(window.name);
                    var body = index == undefined ? $('body'): parent.layer.getChildFrame('body', index);
                    for (var item in msg) {
                        body.find('[data-valmsg-for="' + item + '"]').attr('class', 'field-validation-error').html('<span id="' + item + '-error" class>' + msg[item] + '</span>');
                    }
                }
                parent.layer.close(index1);
            },
            error: function (res) {
                layer.msg('服务器错误!', { icon: 6, time: 1000 });
                layer.close(index);
                console.error(msg.errorMsg);
            }
        });
    }

});

function layer_full(title, url) {
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}